<template>
	<!-- 用户管理-分配账套 -->
	<v-dialog v-model="setAccountBookModel.showDialog" width="800" style="height: 500px;">
		<v-card>
			<v-card-title>分配账套</v-card-title>
			<v-card-text>
				<v-combobox v-model="accountBookIdList" :items="accountBookItems" label="选择账套" multiple chips>
				</v-combobox>
			</v-card-text>
			<v-card-actions>
				<v-spacer></v-spacer>
				<v-btn color="success" class="mr-4" text @click="setAccountBookSubmit">确认</v-btn>
				<v-btn color="warning" class="mr-4" text @click="close">取消</v-btn>

			</v-card-actions>
		</v-card>
	</v-dialog>
</template>


<script>
	export default {
		name: 'set_accountbook',
		props: {
			callBackLoadUsers: {
				type: Function, //参数类型：函数
			},
			setAccountBookModel: {
				showDialog: false,
				userIdList: []
			},
		},
		data: () => ({
			accountBookItems: [],
			accountBookIdList: []
		}),
		watch: {
			setAccountBookModel: {
				handler(newVal) {
					this.$emit('update:setAccountBookModel', newVal)
					this.loadAccountBookList()
				},
				deep: true
			},
		},

		methods: {
			async loadAccountBookList() {

				if (!this.setAccountBookModel.showDialog) {
					return
				}

				var response = await this.$httpRequest({
					url: `SPM/User/GetAccBooksToSelect`,
					method: 'get'
				})

				if (response.status == 1) {
					this.accountBookItems = response.data
				} else {
					this.$message.error(response.info)
				}

			},
			async setAccountBookSubmit() {
				if (this.accountBookIdList.length <= 0) {
					this.$message.error('请选择需要分配的账套！')
					return
				}
				var response = await this.$httpRequest({
					url: `SPM/User/SetUsersAccountBook`,
					method: 'post',
					data: {
						'AccountBookIdList': this.accountBookIdList.map(r => {
							return r.value
						}),
						'UserIdList': this.setAccountBookModel.userIdList
					}
				})

				if (response.status == 1) {
					this.$message.success(response.info)
					this.$props.callBackLoadUsers()
					this.close()
				} else {
					this.$message.error(response.info)
				}
			},
			close() {
				this.setAccountBookModel.showDialog = false
				this.accountBookItems = []
				this.accountBookIdList = []
			},
		}
	}
</script>

<style>
</style>
